// pages/user/coupon/coupon.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
var WxParse = require('../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scanning:'../../../images/scanning.png',
    coupon0: '../../../images/coupon0.png',
    coupon1: '../../../images/coupon1.png',
    go_right_red: '../../../images/go_right_red.png',
    list:[],
    putTypes:[
      {
        id:1,
        name:'新用户注册'
      },
      {
        id: 2,
        name: '线下发放'
      },
      {
        id: 3,
        name: '免费领取'
      },
      {
        id: 4,
        name: '抽奖领取'
      },
      
    ],
    couponNum:'',//输入框劵码
    empty: true,
    hasMore: false, //数据是否加载完成
    loading: false,
    load_show: false,
    hasCoupon: 0, // 是否下单跳转 1表示下单选择优惠券，0默认我的优惠券
    money: 0, // 金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 判断是否下单页面跳转过来
    if (options.money) {
      that.setData({
        money: options.money,
        hasCoupon: 1
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    _PAGE = 1;
    // 表示从下单页面跳转过来
    if (that.data.hasCoupon == 1) {
      that.getGoodsCoupon(); // 获取商品购买可用优惠券列表
    } else {
      that.getCouponList(); // 获取优惠券列表
    }
  },

  // 获取商品购买可用优惠券列表
  getGoodsCoupon: function () {
    var that = this;
    var total_money = that.data.money; // 商品金额
    // ajax 请求
    app.ajax({
      url: 'User/Coupon/getCoupon',
      data: { total_money: total_money, page: _PAGE },
      success: function (res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              list: res.data.data,
              empty: false
            });
          } else {
            that.setData({
              list: that.data.list.concat(res.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });

        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })
        console.log('商品购买获取优惠券', res);
      }
    })
  },

  // 立即使用
  useing(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var money = e.currentTarget.dataset.money;
    var name = e.currentTarget.dataset.name;
    if (that.data.hasCoupon == 1) {
      wx.setStorageSync('coupon_id', id); // 优惠券ID
      wx.setStorageSync('coupon_money', money); // 优惠券金额
      wx.setStorageSync('coupon_name', name); // 优惠券名称
      wx.navigateBack({
        delta: 1
      })
    }
  },

  // 购买商品时不使用优惠券
  cancelCoupon: function () {
    wx.setStorageSync('coupon_id', ''); // 优惠券ID
    wx.setStorageSync('coupon_money', 0); // 优惠券金额
    wx.setStorageSync('coupon_name', '使用'); // 优惠券名称
    wx.navigateBack({
      delta: 1
    })
  },

  // 获取我的优惠券列表
  getCouponList() {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'User/Coupon/meList',
      data: { 
        type: 1,
        page: _PAGE, 
        pageSize: _PAGESIZE 
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              list: res.data.data,
              empty: false
            });
          } else {
            that.setData({
              list: that.data.list.concat(res.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });

        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })
      }
    })   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    _PAGE++;
    // 表示从下单页面跳转过来
    if (that.data.hasCoupon == 1) {
      that.getGoodsCoupon(); // 获取商品购买可用优惠券列表
    } else {
      that.getCouponList(); // 获取优惠券列表
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('1')
  },

  // 绑定输入
  bindInput: function(e){
    var that = this;
    var num = e.detail.value;
    that.setData({
      couponNum: num
    })
  },

  // 兑换
  exchange: function(){
    var that = this;
    var couponNum = that.data.couponNum;
    if (couponNum == ''){
      app.showToast("请输入劵码");
      return;
    }
    app.ajax({
      url:'User/Coupon/exchangeCoupon',
      data: { code: couponNum },
      success: function(res){
        if(res.data.code == 1000){
          app.showToast('兑换成功');
          _PAGE = 1;
          that.getCouponList();
        }else{
          app.showToast(res.data.message);
        }

      }
    })

  },

  // 搜索
  search: function () {
    var that = this;
    console.log('asd');
  },

  // 使用优惠券
  useCoupon: function(e){
    var that = this;
    var couponId = e.currentTarget.dataset.id;
    console.log(couponId);
    app.showToast(couponId+'已被使用');
  },

  // 使用说明
  bindInstructions: function (e) {
    var that = this;
    wx.navigateTo({
      url: '../instructions/instructions'
    })

    // var instruction = e.currentTarget.dataset.instruction;
    // console.log(e);
    // app.showModal("", instruction, function () { }, function () { });
  },

  // 扫码
  scanning:function(){
    var that = this;
    wx.scanCode({
      success:(res)=>{
        app.showToast('扫码成功');
        console.log(res)
        that.setData({
          couponNum: res.result
        })       
      },
      fail:(res)=>{
        app.showToast('扫码失败，请重试！');
      }
    })
  },

  //查看已失效的券
  lookOld:function(){
    wx.navigateTo({
      url: '../couponOld/couponOld',
    })
  },

})